/**
 * @author Teacher Gong
 * @note 修改密码界面
 */

package team.wh1tegive.cg;

import team.wh1tegive.misc.Log;
import team.wh1tegive.user.User;
import team.wh1tegive.user.UserAction;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

class ModifyPwdFrame extends JFrame implements ActionListener {
    private JLabel l_oldPWD, l_newPWD, l_newPWDAgain;
    private JPasswordField t_oldPWD, t_newPWD, t_newPWDAgain;
    private JButton b_ok, b_cancel;
    private String username;
    private User user;

    public ModifyPwdFrame(User user) {
        super("修改密码");
        this.user = user;
        this.username = user.getName();
        l_oldPWD = new JLabel("我的旧密码:");
        l_newPWD = new JLabel("设置新密码:");
        l_newPWDAgain = new JLabel("确认新密码:");
        t_oldPWD = new JPasswordField(15);
        t_newPWD = new JPasswordField(15);
        t_newPWDAgain = new JPasswordField(15);
        b_ok = new JButton("确定");
        b_cancel = new JButton("取消");
        Container c = this.getContentPane();
        c.setLayout(new FlowLayout());
        c.add(l_oldPWD);
        c.add(t_oldPWD);
        c.add(l_newPWD);
        c.add(t_newPWD);
        c.add(l_newPWDAgain);
        c.add(t_newPWDAgain);
        c.add(b_ok);
        c.add(b_cancel);
        b_ok.addActionListener(this);
        b_cancel.addActionListener(this);
        this.setResizable(false);
        this.setSize(250, 150);
        Dimension screen = this.getToolkit().getScreenSize();
        this.setLocation((screen.width - this.getSize().width) / 2, (screen.height - this.getSize().height) / 2);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setVisible(true);
        Log.writeLog(username + " Attempting to Change Password.");
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                Log.writeLog(username + " decided NOT to Change Password.");
            }
        });
    }

    public void actionPerformed(ActionEvent e) {
        if (b_cancel == e.getSource()) {
            Log.writeLog(username + " decided NOT to Change Password.");
            this.dispose();
        } else if (b_ok == e.getSource()) {  //修改密码
            String old = new String(t_oldPWD.getPassword());
            String new1 = new String(t_newPWD.getPassword());
            String new2 = new String(t_newPWDAgain.getPassword());

            if (!new1.equals(new2)) {  //两次输入新密码不一致
                JOptionPane.showMessageDialog(null, "两次输入新密码不一致。", "警告", JOptionPane.ERROR_MESSAGE);
                t_oldPWD.setText("");
                t_newPWD.setText("");
                t_newPWDAgain.setText("");
                Log.writeError("The User's new Password can't be Verified. Two times are different.");
            } else if (!UserAction.OauthCheck(username, old)) { //旧密码验证不功
                JOptionPane.showMessageDialog(null, "旧密码输入错误，请仔细检查！", "警告", JOptionPane.ERROR_MESSAGE);
                t_oldPWD.setText("");
                t_newPWD.setText("");
                t_newPWDAgain.setText("");
                Log.writeError("Old Password Verification Failed.");
            } else { // 开始改密码
                if (user.pwdUpdate(new1)) {
                    JOptionPane.showMessageDialog(null, "密码更改成功！", "成功", JOptionPane.INFORMATION_MESSAGE);
                    Log.writeLog(username + " Successfully Updated Password.");
                    this.dispose();
                } else {
                    JOptionPane.showMessageDialog(null, "密码更改失败，请详情查看日志。", "警告", JOptionPane.ERROR_MESSAGE);
                    Log.writeError(username + " Change Password Failed.");
                    t_oldPWD.setText("");
                    t_newPWD.setText("");
                    t_newPWDAgain.setText("");
                }
            }
        }
    }
}